package com.mbc.web.common.client.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.mbc.common.entities.BranchType;
import com.mbc.web.common.shared.dto.BranchTypeDataListDTO;
import com.mbc.web.common.client.ClientSingleton;
import com.mbc.web.common.client.CommonControllerMgr;
import com.mbc.web.common.client.CommonMessage;
import com.mbc.web.common.client.ViewUtils;
import com.mbc.web.common.client.ui.AbstractListView;
import com.mbc.web.common.client.ui.control.GridLabelColumn;
import com.mbc.web.common.client.ui.control.GridView;
import com.mbc.web.common.client.ui.control.MButton;
import com.mbc.web.common.client.ui.control.Pager;
import com.mbc.web.common.shared.HttpStatus;

public class SearchBranchType extends AbstractListView<BranchType> {

	@UiField
	GridView gvDisplay;
	@UiField
	Button btnSearch;
	@UiField
	Pager pager;
	@UiField
	TextBox txtKeyword;
	@UiField
	MButton btnAddNew;
	@UiField ListBox lstfield;

	public static final byte SAVE = 1;
	public static final byte DELETE = 0;
	
	private GridLabelColumn colBranchTypeCode = new GridLabelColumn(
			"Branch Type ID");
	private GridLabelColumn colName = new GridLabelColumn("Name");
	private GridLabelColumn colDescription = new GridLabelColumn("Description");

	private static SearchBranchTypeUiBinder uiBinder = GWT
			.create(SearchBranchTypeUiBinder.class);

	interface SearchBranchTypeUiBinder extends
			UiBinder<Widget, SearchBranchType> {
	}

	public SearchBranchType() {
		initWidget(uiBinder.createAndBindUi(this));
		initialize();

	}

	public SearchBranchType(String message, byte status){
		initWidget(uiBinder.createAndBindUi(this));
		initialize();
		if(status == SAVE)
			ViewUtils.showSuccessMessage(message + " " + CommonMessage.saved_message);
		else if(status == DELETE)
			ViewUtils.showSuccessMessage(message + " " + CommonMessage.deleted_message);
	}
	
	public SearchBranchType(String title) {
		initWidget(uiBinder.createAndBindUi(this));
		this.title = title;
		initialize();
	}

	protected void bindDisplay(List<BranchType> list) {
		gvDisplay.removeAllRows();
		for (BranchType l_Type : list) {
			gvDisplay.addRow(new String[] { l_Type.getBranchTypeCode(),
					l_Type.getName(), l_Type.getDescription() });

		}
		this.generateList();
	}

	@Override
	protected void initialize() {
		
		lstfield.addItem("Branch Type Code","BranchTypeCode");
		lstfield.addItem("Name","Name");
		lstfield.addItem("Description","Description");
		
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {

			@Override
			public void execute() {
				txtKeyword.setFocus(true);

			}
		});
		colBranchTypeCode.styleName = "clickable-col";
		colBranchTypeCode.clickHandler = new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (isModal) {
					selectedEntity = dataList.get(gvDisplay
							.getSelectedRowIndex());
					closeModal();
				} else
					ClientSingleton.changeView(new FrmBranchType(dataList
							.get(gvDisplay.getSelectedRowIndex())));
			}
		};
		gvDisplay.addColumn(colBranchTypeCode);
		gvDisplay.addColumn(colName);
		gvDisplay.addColumn(colDescription);

		pager.btnPrevious_ClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				search();
			}
		});

		pager.btnNext_ClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				search();
			}
		});

		pager.btnLast_ClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				search();
			}
		});

		onBtnSearchClick(null);

	}

	@Override
	protected void search() {
		ViewUtils.startAjaxLoader(CommonMessage.loading_message);
		CommonControllerMgr.findBranchTypes(prepareCri(),
				findBranchTypesCallback());
	}

	private AsyncCallback<BranchTypeDataListDTO> findBranchTypesCallback() {
		return new AsyncCallback<BranchTypeDataListDTO>() {

			@Override
			public void onSuccess(BranchTypeDataListDTO result) {
				ViewUtils.stopAjaxLoader();
				if (result.getHttpStatus() == HttpStatus.OK) {
					dataList = result.getDataList().getEntityList();
					pager.totalRecord = result.getDataList().getTotalRecord();
					bindDisplay(dataList);
					pager.prepareVisible();
				}
			}

			@Override
			public void onFailure(Throwable caught) {

			}
		};
	}

	private BranchType prepareCri() {
		BranchType l_cri = new BranchType();
		l_cri.setKeyword(txtKeyword.getValue());
		l_cri.setIndex(pager.index);
		l_cri.setSize(10);
		l_cri.setFieldname(lstfield.getValue(lstfield.getSelectedIndex()));
		return l_cri;
	}

	@UiHandler("btnSearch")
	void onBtnSearchClick(ClickEvent event) {
		pager.index = 0;
		search();
	}

	@UiHandler("btnAddNew")
	void onBtnAddNewClick(ClickEvent event) {
		ClientSingleton.changeView(new FrmBranchType());
	}

	@UiHandler("txtKeyword")
	void onTxtKeywordKeyDown(KeyDownEvent event) {
		if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
			onBtnSearchClick(null);
		}
	}
}
